
global root_dir = "`1'"
include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A42_innovation_categories.log, replace name(tabl)

capture noi {
	
	qui do ${code_dir}/config/tabletools.do

	use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear

	clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a
	clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
	clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a
	clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
	clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a

	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearindustry = group(year industry) if year <= 2009
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009


	estimates clear
	foreach depvar in auto95 autoX95 autonol95 auto80 autm90 autm80 robo90 robo80 CNC90 CNC80  {
		clonevar spilloversown = spill`depvar'${ttt}_1995_a
		clonevar spilloversother = spillN`depvar'${ttt}_1995_a
		clonevar spilloversownzero = spill`depvar'${ttt}_1995_a0
		clonevar spilloversotherzero = spillN`depvar'${ttt}_1995_a0
		clonevar stockown = k`depvar'_${ttt} 
		clonevar stockownzero = k`depvar'_${ttt}0  
		clonevar stockother = kNOT_`depvar'_${ttt} 
		clonevar stockotherzero = kNOT_`depvar'_${ttt}0
		bys lse_id : egen _total_`depvar'_${ttt}_1995 = sum(`depvar'_${ttt}) if year>=1995+2 & year <= 2009+2
		bys lse_id : egen total_`depvar'_${ttt}_1995 = max(_total_`depvar'_${ttt}_1995)
		drop _total_`depvar'_${ttt}_1995
		sort lse_id year

		ppmlhdfe F2.`depvar'_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_`depvar'_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
		estadd local f "\yes"
		estadd local iy "\yes"
		estadd local cy "\yes"
		estadd local has_stockspill "\yes"
		estadd local obs "{\num{`e(N)'}}"
		estadd local firms "{\num{`e(N_clust)'}}"
		if ("`depvar'" == "auto95") estimates store col_0
		if ("`depvar'" != "auto95") estimates store col_`depvar'
		drop spilloversown* spilloversother* stock*
	}



	qui do ${code_dir}/config/tabletools.do
	setlabels

	dhoztab * using ${tab_dir}/appendix/Table_A42_innovation_categories.tex, b(%10.2f) se(%10.2f) nogaps replace nonumbers nonotes nolines nomtitles sfmt(a1) scalars("has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label drop(*zero _cons spill* stock*) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC) showdepvarinfo mlabel("{(0)}" "{(1)}" "{(2)}" "{(3)}" "{(4)}" "{(5)}" "{(6)}" "{(7)}" "{(8)}" "{(9)}") depvar("Auto95") depvarlist(Auto95 AutoX95 Auto95_noL Auto80 Automat*90 Automat*80 Robot90 Robot80 CNC90 CNC80 ) refcat(LSW "Foreign:", nolabel) notes("This table analyzes the effect of wages on different automation innovation categories. AutoX95 excludes the C/IPC codes which we added when defining the machinery technological field. Auto95_noL measures automation with exactly the same procedure as auto95 but excluding the labor keywords from the list in automation keywords. Auto80 lowers the threshold to define automation innovation to the 80th percentile of the C/IPC 6-digit distribution. Automat*90 and Automat*80 only count words associated with automat. Robot90 and Robot80 only count words associated with robot. CNC90 and CNC80 words associated with CNC. 90 and 80 refer to the thresholds used to define the corresponding technology categories, which are the 90th and 80th percentile of the distribution of automation keywords for 6-digit C/IPC codes. The macroeconomic variables are the normalized foreign variables as defined in the text. Stocks and spillovers are computed with respect to the dependent variable. All regressions include firm fixed effects, industry-year, and country-year fixed effects. Standard errors are clustered at the firm-level and reported in parentheses.")


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}